//
//  WBHomeViewController.swift
//  weibo
//
//  Created by HanXujie on 2017/4/9.
//  Copyright © 2017年 thinker. All rights reserved.
//

import UIKit

private let cellID = "cellID"


class WBHomeViewController: WBBaseViewController {
    
    
    /// 列表视图模型
    fileprivate lazy var listViewModel = WBStatusListViewModel()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
    }
    
    
    /// 加载数据
    override func loadData() {
        
        listViewModel.loadStatus(pullup: self.isPullup) { (isSuccess, shouldRefresh) in
            
            
            // 结束刷新控件
            self.refreshControl?.endRefreshing()
            
            // 恢复上拉刷新标记
            self.isPullup = false
            
            // 刷新表格
            if shouldRefresh {
                
                self.tablewView?.reloadData()
            }
            
            
        }
        
    }
    
    /// 好友按钮点击监听方法:显示好友
    @objc fileprivate func showFriends(){
        
        let vc = WBDemoViewController()
        
        // 在开发的时候容易忽略此方法
        //        vc.hidesBottomBarWhenPushed = true
        
        navigationController?.pushViewController(vc, animated: true)
    }
    
    
}

// MARK: - 表格的数据源代理方法
extension WBHomeViewController{
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return listViewModel.statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        // 取cell
        let cell = tablewView?.dequeueReusableCell(withIdentifier: cellID, for: indexPath)
        
        // 设置cell
        cell?.textLabel?.text = listViewModel.statusList[indexPath.row].text
        
        // 返回
        return cell!
    }
    
}

// MARK: - 设置界面
extension WBHomeViewController{
    
    override func setupTableView() {
        super.setupTableView()
        
        // 设置导航栏按钮
        // 无法高亮
        //        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "好友", style: .plain, target: self, action: #selector(showFriends))
        
        
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tablewView?.register(UITableViewCell.self, forCellReuseIdentifier: cellID)
        
        
    }
    
}
